package com.lyz.myself.service.ServiceImpl;

import com.lyz.myself.config.utils.util.DateUtil;
import com.lyz.myself.controller.BaseController;
import com.lyz.myself.mapper.LogMapper;
import com.lyz.myself.mapper.RLeadsLogMapper;
import com.lyz.myself.pojo.Po.Leads;
import com.lyz.myself.pojo.Po.Log;
import com.lyz.myself.pojo.Po.RLeadsLog;
import com.lyz.myself.service.LeadsService;
import com.lyz.myself.service.LogService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @program: myself
 * @Date: 2019/3/1 9:31
 * @Author: Mr.Deng
 * @Description:
 */
@Service
public class LogServiceImpl extends BaseController implements LogService {

    @Autowired
    LogMapper logMapper;

    @Autowired
    RLeadsLogMapper rLeadsLogMapper;

    @Autowired
    LeadsService leadsService;

    /**
     * 添加日志
     *
     * @param record
     * @return
     */
    @Transactional
    @Override
    public int insertSelective(Log record) {
        record.setUpdate_date(DateUtil.timeMillis());
        record.setRole_id(getUser().getRole_id());
        record.setCreate_date(DateUtil.timeMillis());
        record.setCategory_id(0);
        record.setAbout_roles("");
        record.setAbout_roles_name("");
        record.setComment_id(0);
        int insertLog = logMapper.insertSelective(record);
        if (insertLog > 0) {
            RLeadsLog RLeadsLog = new RLeadsLog();
            RLeadsLog.setCompany_id(record.getCompany_id());
            RLeadsLog.setLeads_id(record.getLeads_id());
            RLeadsLog.setLog_id(record.getLog_id());
            int insertRLeadsLog = rLeadsLogMapper.insert(RLeadsLog);
            Leads Leads = new Leads();
            Leads.setNextstep(record.getNextstep());
            Leads.setNextstep_time(record.getNextstep_time());
            Leads.setCompany_id(record.getCompany_id());
            Leads.setLeads_id(record.getLeads_id());
           int updateLeads =  leadsService.updateByPrimaryKeySelective(Leads);
            if (insertRLeadsLog > 0&&updateLeads>0) {
                return insertRLeadsLog;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }


    /**
     *删除日志
     * @mbg.generated 2019-03-01
     */
    @Transactional
    @Override
   public int deleteByPrimaryKey(List<Integer> list){
      int deleteLog =  logMapper.deleteByPrimaryKey(list);
       if (deleteLog > 0) {
          int deleterLeadsLogMapper =  rLeadsLogMapper.deleteByPrimaryKey(list);
           if (deleterLeadsLogMapper > 0) {
               return deleterLeadsLogMapper;
           } else {
               return 0;
           }
       } else {
           return 0;
       }
   }

    /**
     *修改日志
     * @mbg.generated 2019-03-01
     */
   public int updateByPrimaryKeySelective(Log record){
       record.setUpdate_date(DateUtil.timeMillis());
       int updateLog =  logMapper.updateByPrimaryKeySelective(record);
       if (updateLog > 0) {
           return updateLog;
       } else {
           return 0;
       }
   }

    /**
     * 根据线索id查询沟通日志
     * @param leads_id
     * @return
     */
    public List<Map<String, String>> selectLog(Integer leads_id,Integer company_id){
        return logMapper.selectLog(leads_id,company_id);
    }
}
